package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.awt.datatransfer.FlavorMap;
import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    //添加菜品
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //添加1个菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //添加以后菜品的id
        Long id = dish.getId();

        List<DishFlavor> flavor = dishDTO.getFlavors();
        if (flavor != null && flavor.size() > 0) {
            //添加口味  可无可多个
            flavor.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorsMapper.insert(flavor);
        }


    }

    //分页查询
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //批量删除
    @Transactional
    public void delete(List<Long> ids) {
        //是否关联了套餐
        int count= dishMapper.setmealById(ids);
        if(count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
//        if(setmealIds !=null && setmealIds.size()>0){
//            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
//        }


//        是否起售
      List<Dish> dishs = dishMapper.statusById(ids);
        for (Dish dish : dishs) {
            Integer status = dish.getStatus();
            if(status==1){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        }

//        for (Long id : ids) {
//            Dish dish = dishMapper.getById(id);
//            if(dish.getStatus()==1){
//                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
//            }
//
//        }


        //删除菜品信息
        dishMapper.delete(ids);
//        for (Long id : ids){
//            dishMapper.deleteById(id);
//            dishFlavorsMapper.deleteByDishID(id);
//        }
        //删除关联的口味信息
        dishFlavorsMapper.delete(ids);
    }

    @Override
    public DishVO dishById(Long id) {
        DishVO dishVO = new DishVO();
        //dish的基本信息
        Dish dish= dishMapper.getById(id);
        //关联的口味信息
       List<DishFlavor> dishFlavor=dishMapper.flavorById(id);
       //加到dishvo里面
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Long id = dishDTO.getId();
        //改菜品口味
        //1先删除
        dishFlavorsMapper.deleteByDishID(id);
        //2添加
        List<DishFlavor> flavor = dishDTO.getFlavors();
        if (flavor != null && flavor.size() > 0) {
            //添加口味  可无可多个
            flavor.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorsMapper.insert(flavor);
        }

        //改菜品的基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);

        dishMapper.update(dish);
    }

}
